<?
    final class Principio_Model_SettingMapper extends Principio_Mapper
    {
        /*
         * Settings
         */
        protected static $db_table = null;
        protected static function getDbTableName() { return "Setting"; }



        // Settings
        const SETTING_HOSTNAME = "hostname";

        // TFTP
        const SETTING_TFTP_DEFAULT_CONFIG = "tftp_default_config";
        const SETTING_TFTP_PATH_TO_INITIAL_CONFIGS = "tftp_path_to_initial_configs";

        // DHCP
        const SETTING_DHCP_RESTART_COMMAND = "dhcp_restart_command";
        const SETTING_DHCP_CONFIG_FILE = "dhcp_config_file";

        // Templates
        const SETTING_GLOBAL_TEMPLATE_GLOBAL = "template_global";
        const SETTING_GLOBAL_TEMPLATE_EXT = "template_ext";


        public static function fetch($setting)
        {
            try
            {
                $select = self::getDBTable()->select()->where("caption=?", $setting);
                $values = self::getDBTable()->fetchRow($select);
                return new Principio_Model_Setting($values->toArray());
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function save(Principio_Model_Setting $model_setting)
        {
            try
            {
                $data = ['value' => $model_setting->getValue()];
                return self::getDBTable()->update($data, ['id=?' => $model_setting->getID()]);
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };

        }


        /*
        * Profile Rules
        */
        const PROFILE_RULE_HTTP = 1;
        const PROFILE_RULE_FIRMWARE = 2;

        const PROFILE_RULE_USE_AMP = 0;
        const PROFILE_RULE_DONT_USE_AMP = 1;

        private static $profile_rules_amp_array = array(
            self::PROFILE_RULE_USE_AMP => "&amp;",
            self::PROFILE_RULE_DONT_USE_AMP => "&"
        );

        public static function getProfileRuleBase($amp_flag = self::PROFILE_RULE_USE_AMP)
        {
            return new Principio_Model_Setting('/prv?mac=$MAU'.self::$profile_rules_amp_array[$amp_flag].'s_id=$SN'.self::$profile_rules_amp_array[$amp_flag].'ip=$IP');
        }

        public static function getProfileRule($rule, $amp_flag = self::PROFILE_RULE_USE_AMP)
        {
            return new Principio_Model_Setting("http://".self::fetch(self::SETTING_HOSTNAME)->getValue().self::getProfileRuleBase($amp_flag)->getValue().self::$profile_rules_amp_array[$amp_flag]."s=".$rule);
        }

        public static function getProfileRuleHTTP($amp_flag = self::PROFILE_RULE_DONT_USE_AMP) { return self::getProfileRule(self::PROFILE_RULE_HTTP, $amp_flag); }
        public static function getProfileRuleFirmware($amp_flag = self::PROFILE_RULE_USE_AMP) { return self::getProfileRule(self::PROFILE_RULE_FIRMWARE, $amp_flag); }


        // TODO : fix these
        public static function getAudioCodecs() { return "Default is G711a"; }
    }
?>